package com.ilongsay.controller;

import com.ilongsay.entity.*;
import com.ilongsay.exception.NosuchException;
import com.ilongsay.service.IBillSqliteService;
import com.ilongsay.service.ISettingService;
import com.ilongsay.service.ISixErrorAdjustService;
import com.ilongsay.service.ISplitService;
import com.ilongsay.utils.CalcUtils;
import com.ilongsay.utils.DateUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @Author ilongsay
 * @Email ilongsay@163.com
 * @Copyright 版本所有，侵权必究！
 * @Datetime 2018-12-09 21:41
 * @Describution
 */
@Controller
@RequestMapping("setting")
public class SettingController {

    @Autowired
    private ISettingService settingService;

    @Autowired
    private IBillSqliteService billSqliteService;

    @Autowired
    private ISixErrorAdjustService sixErrorAdjustService;

    @Autowired
    private ISplitService splitService;

    @RequestMapping(value = "/chaifen", method = RequestMethod.POST)
    @ResponseBody
    public Object chaiFenSetting() {
        AjaxObject ajaxObject = new AjaxObject();
        ChaiFenSetting setting = settingService.findChaifenSetting();
        ajaxObject.setData(setting);
        ajaxObject.setCode("0");
        return ajaxObject;
    }

    /**
     * @param chaiFenSetting
     * @return java.lang.Object
     * 这里主要是为了测试这个拆分设置的更新是否可用！
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object chaifen(ChaiFenSetting chaiFenSetting) throws InvocationTargetException, IllegalAccessException {
        String[] split = chaiFenSetting.getBillCode().split("\\,");
        AjaxObject ajaxObject = null;

        if (chaiFenSetting.getMingxi().equals("true")) {
            //按明细拆分订单
            ajaxObject = splitService.splitByMX(chaiFenSetting);
        } else if (chaiFenSetting.getShuilv().equals("true")) {
            //按税率拆分订单
            ajaxObject = splitService.splitByTaxRate(chaiFenSetting);
        } else if (chaiFenSetting.getAmount() != null) {
            //按金额拆分订单
            ajaxObject = splitService.splitByAmount(chaiFenSetting);
        }
        settingService.updateChaifenSetting(chaiFenSetting);
        return ajaxObject;

    }

    /**
     * @param
     * @return java.lang.Object
     * 查询系统设置
     */
    @ResponseBody
    @RequestMapping("/system")
    public Object system() {
        return settingService.querySetting();
    }

    /**
     * @param limitType
     * @return java.lang.Object
     * 更新限额
     */
    @ResponseBody
    @RequestMapping(value = "/updatelimit", method = RequestMethod.POST)
    public Object updateLimit(LimitType limitType) {
        return settingService.updateSysSetting(limitType);
    }

    /**
     * @param type 发票类型
     * @return java.lang.Object
     * 查询限额设置
     */
    @ResponseBody
    @RequestMapping("/queryLimitSetting")
    public Object queryLimitSetting(@RequestParam("type") String type) {
        return settingService.queryLimitSetting(type);
    }

    @ResponseBody
    @RequestMapping("/preserve")
    public Object preserveSysSetting(Setting setting) {
        return settingService.preserveSyeSetting(setting);
    }

    /**
     * @param
     * @return java.lang.Object
     * 查询误差调整的参数进行回显，日后需要根据用户的参数来进行回显
     */
    @ResponseBody
    @RequestMapping("/erroradjust")
    public Object queryErrorSetting() {
        return settingService.queryErrorAdjustSetting();
    }

    /**
     * @param errorAdjust 调整的参数
     * @return java.lang.Object
     * 进行误差调整的请求
     */
    @ResponseBody
    @RequestMapping("/adjusterror")
    public Object errorAdjust(ErrorAdjust errorAdjust,
                              @RequestParam("billCode")String billCode){
        System.out.println(billCode+errorAdjust);
        return sixErrorAdjustService.autoSixErrorAdjust(errorAdjust,billCode);
    }
}
